package com.zretc.emptraining.daoimpl;

import java.time.LocalDateTime;
import java.util.List;

import com.zretc.emptraining.dao.ManagerAppDao;
import com.zretc.emptraining.dbtools.DBTools;
import com.zretc.emptraining.dbtools.PageData;
import com.zretc.emptraining.entity.ManagerApp;

public class ManagerAppDaoImp implements ManagerAppDao {

	@Override
	public boolean addManagerApp(ManagerApp managerApp) {
		// 获取当前时间
		LocalDateTime now = LocalDateTime.now();
		// 不为空执行添加
		if (managerApp != null) {
			String sql = "insert into managerapp values(0,?,?,?,now(),null,?)";
			int n = DBTools.getUpdate(sql, managerApp.getDeptNo(), managerApp.getDeptName(),
					managerApp.getApplyReason(),  "0");
			return n > 0;
		}

		return false;
	}

	@Override
	public boolean delManagerApp(int managerappNo) {
		String sql = "update managerapp set applyState = ? where managerappNo =? and applyState != ?";
		int n = DBTools.getUpdate(sql, "2", managerappNo, "1");
		return n > 0;
	}

	@Override
	public boolean passManagerApp(int managerappNo) {
		LocalDateTime now = LocalDateTime.now();
		String sql = "update managerapp set passTime = now(),applyState = ? where managerappNo = ?";
		int n = DBTools.getUpdate(sql, "1", managerappNo);
		return n > 0;
	}

	@Override
	public PageData<ManagerApp> getManagerAppsByLike(int page, int pageSize, String keyword) {
		PageData<ManagerApp> pd = new PageData<ManagerApp>();
		if (page < 1) {
			page = 10;
		}
		if (pageSize < 1) {
			pageSize = 10;
		}
		if (keyword != null) {
			String sql = "SELECT m.managerappNo,m.deptNo,(select deptName from dept where m.deptNo=dept.deptNo) as ndn ,m.deptName ,m.applyReason,m.applyTime,m.passTime,m.applyState from managerapp m where m.deptName like ?";
			pd = DBTools.exQueryByPage(sql, ManagerApp.class, page, pageSize, "%" + keyword + "%");
		}
		return pd;

	}

	@Override
	public boolean UpdateManagerApp(ManagerApp managerApp) {
		LocalDateTime now = LocalDateTime.now();
		String sql = "update managerapp set deptName = ?,applyReason = ?,applytime = ? where managerappNo = ?";
		int n = DBTools.getUpdate(sql, managerApp.getDeptName(), managerApp.getApplyReason(), now,
				managerApp.getManagerappNo());
		return n > 0;
	}

	@Override
	public boolean refuseManagerApp(int managerappNo) {
		String sql = "update managerapp set applyState = ?,passTime=? where managerappNo = ?";
		int n = DBTools.getUpdate(sql, "0", null, managerappNo);
		return n > 0;
	}

	@Override
	public boolean delManagerAppByAdmin(int managerappNo) {
		String sql = "delete from managerapp where managerappNo = ?";
		int n = DBTools.getUpdate(sql, managerappNo);
		return n > 0;
	}

	@Override
	public boolean UpdateManagerAppByAdmin(ManagerApp managerApp) {
		LocalDateTime now = LocalDateTime.now();
		String sql = "update managerapp set deptNo = ?, deptName = ?,applyReason = ?,applytime = ? where managerappNo = ?";
		int n = DBTools.getUpdate(sql, managerApp.getDeptNo(), managerApp.getDeptName(), managerApp.getApplyReason(),
				now, managerApp.getManagerappNo());
		return n > 0;

	}

	@Override
	public PageData<ManagerApp> getManagerAppByDeptNo(int page, int pageSize, int deptNo) {
		// String sql = "select * from managerapp where deptNo = ?";
		String sql = "SELECT m.managerappNo,m.deptNo,(select deptName from dept where m.deptNo=dept.deptNo) as ndn ,m.deptName ,m.applyReason,m.applyTime,m.passTime,m.applyState from managerapp m where m.deptNo=?";
		PageData<ManagerApp> pd = new PageData<ManagerApp>();
		pd = DBTools.exQueryByPage(sql, ManagerApp.class, page, pageSize, deptNo);
		return pd;
	}
	
	@Override
	public PageData<ManagerApp> getMnagerAppByDeptNoWithDeptName(int page, int pageSize, int deptNo, String deptName) {
		String sql = " SELECT managerapp.managerappNo,managerapp.deptNo,(select deptName from dept where managerapp.deptNo=dept.deptNo) as ndn ,managerapp.deptName ,managerapp.applyReason,managerapp.applyTime,managerapp.passTime,managerapp.applyState from managerapp where deptNo=? and managerapp.deptName LIKE ? order by applyTime desc" ;
		PageData<ManagerApp> pd = new PageData<ManagerApp>();
		pd = DBTools.exQueryByPage(sql, ManagerApp.class, page, pageSize, deptNo,"%"+deptName+"%");
		return pd;
	}

}
